import { Form, Input } from 'antd';
import { forwardRef, useImperativeHandle } from 'react';
import { isStrongPassword, passwordMessage } from '@/utils';

type UserResetFormProps = {
  userId: number;
  userName: string;
};
// forwardRef
const UserResetForm = forwardRef<any, UserResetFormProps>((props, ref) => {
  const { userName } = props;
  const [form] = Form.useForm();

  // ✅ 暴露方法给父组件
  useImperativeHandle(ref, () => ({
    form, // 暴露 form 实例
    getValues: () => form.getFieldsValue(),
    reset: () => form.resetFields(),
    validate: () => form.validateFields(),
  }));

  return (
    <Form form={form} layout="vertical">
      {/* 用户密码长度必须介于 5 和 20 之间 */}
      <Form.Item
        name="newPassword"
        label={
          <>
            请输入 <strong>{userName}</strong> 新密码
          </>
        }
        rules={[
          {
            required: true,
            // message: "请输入新密码",
            validator(_rule, value, callback) {
              callback(isStrongPassword(value) ? undefined : passwordMessage);
            },
          },
        ]}
      >
        <Input.Password placeholder="请输入新密码" />
      </Form.Item>
    </Form>
  );
});
export { UserResetForm };
